package com.gxuwz.stats.controller;

import com.gxuwz.stats.security.jwt.JwtTokenUtil;
import com.gxuwz.stats.service.FileParser;
import com.gxuwz.stats.service.FileParserFactory;
import com.gxuwz.stats.service.impl.ExcelParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/imports")
public class ImportController {

    @Autowired
    private FileParserFactory fileParserFactory;

    /**
     * 导入用户表格数据
     * @param file
     * @param updateSupport
     * @return
     */
    @PostMapping("/importData")
    public ResponseEntity<Map<String, Object>> importData(
            @RequestParam("file") MultipartFile file,
            @RequestParam("type") String type, // 新增类型参数
            @RequestHeader("Authorization") String authorization,
            @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) {
        try {
            System.out.println("收到的类型="+type);

            // 解析token获取用户信息
            String token = authorization.replace("Bearer ", "");
            JwtTokenUtil jwtTokenUtil = new JwtTokenUtil();
            String username = jwtTokenUtil.getUsernameFromToken(token);
            String role = jwtTokenUtil.getRoleFromToken(token);
            System.out.println("导入角色："+role);
            String department = jwtTokenUtil.getDepartmentFromToken(token);

            FileParser parser = fileParserFactory.getParser("excel");
            // 传递 type 给 parser
            if (parser instanceof ExcelParser) {
                Map<String, Object> result = ((ExcelParser) parser).parse(file, type, username, role, department);
                Map<String, Object> response = new HashMap<>();
                response.put("code", 200);
                response.put("message", "导入成功！");
                response.put("imported", result.get("imported"));
                response.put("ignored", result.get("ignored"));
                return ResponseEntity.ok(response);
            } else {
                parser.parse(file); // 兼容其他类型
            }
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "导入成功，请补充缺失数据！");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 400);
            errorResponse.put("message", "导入失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse);
        }
    }
//    @PostMapping("/importData")
//    public ResponseEntity<Map<String, Object>> importData(
//            @RequestParam("file") MultipartFile file,
//            @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) {
//        try {
//            FileParser parser = fileParserFactory.getParser("excel");
//            parser.parse(file);
////            List<User> users = userService.importExcel(file.getInputStream(), updateSupport);
//            Map<String, Object> response = new HashMap<>();
//            response.put("code", 200);
//            response.put("message", "导入成功");
//            return ResponseEntity.ok(response);
//        } catch (Exception e) {
//            Map<String, Object> errorResponse = new HashMap<>();
//            errorResponse.put("code", 400);
//            errorResponse.put("message", "导入失败: " + e.getMessage());
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse);
//        }
//    }

}
